/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.common.util;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static String PATTEN_YMD = "yyyy-MM-dd";
	public static String PATTEN_HMS = "yyyy/MM/dd HH:mm:ss";
	
	private static SimpleDateFormat getDateParser(String pattern) {
		return new SimpleDateFormat(pattern);
	}

	/**
	 * 将当前时间转换成指定的格式显示
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String dateToString(String pattern) {
		return getDateParser(pattern).format(new Date());
	}

	/**
	 * 将指定格式的时间转换成Date类型
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date stringToDate(String date, String pattern) {
		Date result = null;
		try {
			result = getDateParser(pattern).parse(date);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return result;
	}
	
	/**
	 * 取得指定年月的开始日期
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getMonthBeginDate(int year, int month) {
		Calendar date = Calendar.getInstance();
		date.clear();
		if (month == 1) {//1月的开始日期是1月1号
			date.set(year, month - 1, 1);
		} else {//其他是26号
			date.set(year, month - 2, 26);
		}
		return date.getTime();
	}
	
	/**
	 * 取得指定年月的结束日期
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getMonthEndDate(int year, int month) {
		Calendar date = Calendar.getInstance();
		date.clear();
		if (month == 12) {//12月的结束日期是12月31号
			date.set(year, month - 1, 31);
		} else {//其他是25号
			date.set(year, month - 1, 25);
		}
		return date.getTime();
	}
	
	/**
	 * 取得当前月的开始日期(上月26号)
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date getCurrentMonthBeginDate() {
		Calendar now = Calendar.getInstance();
		return getMonthBeginDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1);
	}
	
	/**
	 * 取得当前月的结束日期(本月25号)
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date getCurrentMonthEndDate() {
		Calendar now = Calendar.getInstance();
		return getMonthEndDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1);
	}
	
	/**
	 * 取得上月的开始日期(上上月26号)
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date getBeforeMonthBeginDate() {
		Calendar now = Calendar.getInstance();
		return getMonthBeginDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH));
	}
	
	/**
	 * 取得上月的结束日期(上月25号)
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date getBeforeMonthEndDate() {
		Calendar now = Calendar.getInstance();
		return getMonthEndDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH));
	}
	
	/**
	 * 获取今天前后n天的日期
	 * @param day 正值是今天后面的n天， 负数是今天前面的n天
	 * @return
	 */
	public static Date getDateFromNow(int day) {
		Calendar date = Calendar.getInstance();
		date.add(Calendar.DAY_OF_MONTH, day);
		return date.getTime();
	}
	
	/**
	 * 获取当前月前后n个月的月份
	 * @param day 正值是当前月后面的n个月， 负数是当前月前面的n个月
	 * @return
	 */
	public static String getMonthFromNow(int month, String fm) {
		Calendar date = Calendar.getInstance();
		date.set(Calendar.DAY_OF_MONTH, 1);
		date.add(Calendar.MONTH, month);
		Format format = new SimpleDateFormat(fm);
		String result = format.format(date.getTime());
		return result;
	}
	
	/**
	 * 获取前一天日期
	 * @param fm 日期格式
	 * @return
	 * @throws DateException
	 */
	public static String stringToBeforeDate(String fm) {
		String result;
		Calendar date = Calendar.getInstance();
		date.add(Calendar.DAY_OF_MONTH, -1);
		Format format = new SimpleDateFormat(fm);
		result = format.format(date.getTime());
		return result;
	}
	
	/**
	 * 获取当天日期
	 * @return
	 */
	public static Date now() {
		return new Date();
	}
	
	/**
	 * 取得两个日期相差的小时数
	 * @param beginDate
	 * @param endDate
	 * @return 
	 */
	public static double getDateOffsetHour(Date beginDate, Date endDate) {
		long hour = 60 * 60 * 1000;
		long diffTime = endDate.getTime() - beginDate.getTime();
		double hourStr = (double)diffTime / hour;
		return hourStr;
	}
	
	/**
	 * 取得两个日期的间隔
	 * @param beginDate
	 * @param endDate
	 * @return d天H小时m分钟
	 */
	public static String getDateOffsetString(Date beginDate, Date endDate) {
		
		long min = 60 * 1000;
		long hour = 60 * min;
		long day = 24 * hour;
		long diffTime = endDate.getTime() - beginDate.getTime();
		int dayStr = (int)Math.floor(diffTime / day);
		diffTime -= dayStr * day;
		int hourStr = (int)Math.floor(diffTime / hour);
		diffTime -= hourStr * hour;
		int minStr = (int)Math.floor(diffTime / min);
		return "" + dayStr + "天" + hourStr + "小时" + minStr + "分钟";
	}
	
	/**
	 * 获取当天日期
	 * @param fm 日期格式
	 * @return
	 * @throws DateException
	 */
	public static String stringToToday(String fm) {
		String result;
		Format format = new SimpleDateFormat(fm);
		result = format.format(new Date());
		return result;
	}
	
	/**
	 * 返回yyyy-mm-dd格式日期
	 * @param date
	 * @return
	 */
	public static String formatString(String date){
		if(StringUtils.isEmpty(date))
			return date;
		if(date.length()<10)
			return date;
		else 
			return date.substring(0, 10);
		
	}
	
	/**
	 * 返回制定格式格式日期
	 * @param date	待转化的日期数据
	 * @param dateFm 日期格式
	 * @return
	 */
	public static String formatString(Date date,String dateFm){
		Format fm = new SimpleDateFormat(dateFm);
		return fm.format(date);
	}
	
	public static void main(String[] args) {
		/*
		Date beginMonth = getCurrentMonthBeginDate();
		System.out.println("beginMonth: " + formatString(beginMonth, "yyyy-MM-dd"));
		Date endMonth = getCurrentMonthEndDate();
		System.out.println("endMonth: " + formatString(endMonth, "yyyy-MM-dd"));
		beginMonth = getBeforeMonthBeginDate();
		System.out.println("beginMonth: " + formatString(beginMonth, "yyyy-MM-dd"));
		endMonth = getBeforeMonthEndDate();
		System.out.println("endMonth: " + formatString(endMonth, "yyyy-MM-dd"));
		Date end = now();
		Date begin = getDateFromNow(-1);
		String message = getDateOffsetString(begin, end);
		System.out.println(message);
		*/
		
		
		System.out.println(getMonthFromNow(0,"yyyy-MM"));
		System.out.println(getMonthFromNow(-2,"yyyy-MM"));
		System.out.println(getMonthFromNow(-5,"yyyy-MM"));
	}
	
}
